<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use app\web\model\AgentModel;
use app\web\model\AddressModel;
use app\web\model\OrderItemsModel;
use app\web\model\OrderModel;
use app\admin\model\Member as MemberModel;
use think\Db;
/**
 * 商品处理
 */
class GoodsController extends IndexBaseController {

	protected $discountGoods_model;
	protected $grabGoods_model;
	protected $pintuanGoods_model;
	protected $scoreGoods_model;
	protected $agent_model;
	protected $member_model;
	protected $address_model;
	

	protected function _initialize() {
		parent::_initialize();
		$this->agent_model = new AgentModel();
		$this->member_model = new MemberModel();
		$this->address_model = new AddressModel();
	}
	/**
	 * 获取单个商品信息
	 */
	 public function getGoods(){
	 	
	 	 $this->add_foots();
	 	 $latitude=input("latitude")?input("latitude"):'25.78226398';
		 $longitude=input("longitude")?input("longitude"):'113.03770447';
	 	 $where=" b.store_id !=0 and b.id=".input("goods_id");
		 $sql ='select a.thumb as logo,a.id,a.store_name,a.address,a.longitude,a.latitude,b.unit,b.nums1,b.goodsid,b.is_pk,b.thumb,b.comment,b.content,b.goods_zs,b.sales,b.sale_price,b.goods_name,b.id as gid,'.
						' ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-a.latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(a.latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-a.longitude*PI()/180)/2),2)))*1000) AS juli'.
						' from long_mall_agent  as a LEFT JOIN long_line_goods as b ON a.id = b.store_id where '.$where;
		 $good = Db::query($sql);
		 $comments=db("mall_comment")->where("agent_id",$good[0]['id'])->count();
		 $where_other=' b.id !='.$good[0]['gid'].' and store_id='.$good[0]['id'];
		 $sql_other ='select a.thumb as logo,a.id,a.store_name,a.address,a.longitude,a.latitude,b.unit,b.nums1,b.goodsid,b.is_pk,b.thumb,b.comment,b.content,b.sales,b.sale_price,b.goods_name,b.id as gid,'.
						' ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-a.latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(a.latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-a.longitude*PI()/180)/2),2)))*1000) AS juli'.
						' from long_mall_agent  as a LEFT JOIN long_line_goods as b ON a.id = b.store_id where '.$where_other.' limit 3';
		 $good_other = Db::query($sql_other);
		 $good[0]['content'] = htmlspecialchars_decode($good[0]['content']);
		 if($good[0]['is_pk']){//如果是套餐
		 	$whe['id'] = array('in',$good[0]['goodsid']);
			$list=Db::name("line_goods")->field("unit,goods_name,sale_price")->where($whe)->select();
			$zslist = db("line_goods")->where("id",$good[0]['goods_zs'])->find();
			$nums=explode(",",$good[0]['nums1']);
			return json(['good'=>$good,"counts"=>$comments[0],"list"=>$list,"nums"=>$nums,'others'=>$good_other,"zslist"=>$zslist]);
		 }else{
		 	return json(['good'=>$good,"counts"=>$comments[0],'others'=>$good_other]);
		 }
		
	 }
	/**
	 * 添加我的足记
	 */
	 public function add_foots(){
	 	$data['uid']=input("uid");
		$data['goods_id']=input("goods_id");
		if($data['uid']){
			$foot=db("mall_foot")->where($data)->find();
			if($foot){
			}else{
				db("mall_foot")->insert($data);
			}
			
		}
	 }
	/**
	 * 积分兑现商品详情
	 */
	public function getscGoods(){
		if(input("uid")){
	 		$this->add_foots();
	 	}
		$good=db('line_goods')->find(input("goods_id"));
		$good['content']=htmlspecialchars_decode($good['content']);
		$sql_other ='select * from long_line_goods where store_id =0  limit 3 ';
		$good_other = Db::query($sql_other);
		return json(['good'=>$good,'others'=>$good_other]);
	}
	/**
	 * 获取限时限量组合商品信息
	 */
	 public function getlimitGoods(){
	 	if(input("uid")){
	 		$this->add_foots();
	 	}
	 	 $where=" b.store_id !=0 and b.id=".input("goods_id");
		 $sql ='select a.thumb as logo,a.id,a.store_name,a.address,b.start_time,b.end_time,b.people_num,b.exchange_bouns,b.unit,b.nums1,b.goodsid,b.is_pk,b.thumb,b.comment,b.content,b.sales,b.sale_price,b.goods_name,b.id as gid'.
						' from long_mall_agent  as a LEFT JOIN long_line_goods as b ON a.id = b.store_id where '.$where;
						
		 $good = Db::query($sql);
		 $where_other=' b.id !='.$good[0]['gid'].' and store_id='.$good[0]['id'];
		 if(input("limit_type")){
		 	$type=input("limit_type");
		 	 if($type==1){
		 		$where_other .=" and b.limit1 =1";
			 }else if($type==2){
			 	$where_other .=" and b.limit2 =1";
			 }else if($type==3){
			 	$where_other .=" and b.is_pk =1 and b.is_zs =0";
			 }
		 }
		 $sql_other ='select a.thumb as logo,a.id,a.store_name,a.address,b.limit1,b.limit2,b.start_time,b.end_time,b.people_num,b.exchange_bouns,b.unit,b.nums1,b.goodsid,b.is_pk,b.thumb,b.comment,b.content,b.sales,b.sale_price,b.goods_name,b.id as gid'.
						' from long_mall_agent  as a LEFT JOIN long_line_goods as b ON a.id = b.store_id where '.$where_other.' limit 3';
		 $good_other = Db::query($sql_other);
		 $good[0]['content'] = htmlspecialchars_decode($good[0]['content']);
		 if($good[0]['is_pk']){//如果是套餐
		 	$whe['id'] = array('in',$good[0]['goodsid']);
			$list=Db::name("line_goods")->field("unit,goods_name,sale_price")->where($whe)->select();
			$nums=explode(",",$good[0]['nums']);
			return json(['good'=>$good,"list"=>$list,"nums"=>$nums,'others'=>$good_other]);
		 }else{
		 	return json(['good'=>$good,'others'=>$good_other]);
		 }
		
	 }
	 //查询商品集合
	public function getlist(){
		$cate_id=input("cate_id");
		if($cate_id){
			$top_list=db('mall_goodstype')->where("pid",$cate_id)->field("id,name")->select();
		}else{
			$top_list=null;
		}
		$where['cate_id']=$cate_id;
		if(input("cate2_id")){
			$where['cate2_id']=input("cate2_id");
		}
		if(input("order")){
				$list=db("line_goods")->where($where)->order(input("order"))->select();
		}else{
				$list=db("line_goods")->where($where)->select();
		}
		return json(['top_list'=>$top_list,'list'=>$list]);		
	}
	/**
	 * 收藏商品
	 */
	public function sc(){
		$where['uid']=input("uid");
		$where['collect_id']=input("goods_id");
		$where['type']=input("type");
		$sc=db("mall_collect")->where($where)->find();
		if($sc){
			return json(["code"=>406,'msg'=>'此商品之前已经收藏过啦！']);
		}
		$data['collect_id']=input("goods_id");
		$data['uid']=input("uid");
		$data['type']=input("type");
		$res=db('mall_collect')->insert($data);
		if($res){
			return json(["code"=>200,'msg'=>'收藏成功']);
		}else{
			return json(["code"=>406,'msg'=>'收藏失败']);
		}
	}
	
	/**
	 * 说说列表
	 */
	function getAssess(){
		$goodsList = Db::name('mall_assess')->order('add_time desc')->limit(4)->select();
		if($goodsList){
			return $this -> toJson(200, '', $goodsList);
		}else{
			return $this -> toJson(405, '没有数据');
		}
	}
	
	/**
	 * 说说的评论
	 */
	function getAssessItem(){
		$assid = request() -> param('assid');
		$goodsList = Db::name('mall_assess')->find($assid);
		$sw['aid']=$assid;
		$sw['uid']=input("uid");
		$dz=db('mall_dzmem')->where($sw)->find();
		$is_dz=2;
		if($dz&&$dz['status']==1){
			$is_dz=1;
		}
		if($goodsList){			
			$goodsLists = Db::name('mall_assessitem')->where('assess_id',$assid)->select();
			$data['asslist'] = $goodsList;
			$data['assitemlist'] = $goodsLists;
			$data['is_dz']=$is_dz;
			return $this -> toJson(200, '', $data);
		}else{
			return $this -> toJson(405, '数据异常');
		}
	}
	
	/**
	 * 点赞
	 */
	function getDianzan(){
		$assid = request() -> param('assid');
		$where['uid']=input("uid");
		$where['aid']=input("assid");
		$dz=db("mall_dzmem")->where($where)->find();
		if($dz){
			if($dz['status']==1){//取消关注
				$dianzan = Db::name('mall_assess')->where('id',$assid)->setDec('dianzan',1);
				$status=2;
			}else{//关注
				$dianzan = Db::name('mall_assess')->where('id',$assid)->setInc('dianzan',1);
				$status=1;
			}
			db("mall_dzmem")->where($where)->update(['status'=>$status]);
			$goodsList = Db::name('mall_assess')->find($assid);
			$zannum =  $goodsList['dianzan'];
			if($dz['status']==1){		
				return $this -> toJson(1, '取消成功', $zannum);
			}else{
				return $this -> toJson(2, '点赞成功', $zannum);
			}
			
		}else{
			$data['uid']=input('uid');
			$data['aid']=input('assid');
			db("mall_dzmem")->insert($data);
			$dianzan = Db::name('mall_assess')->where('id',$assid)->setInc('dianzan',1);
			$goodsList = Db::name('mall_assess')->find($assid);
			$zannum =  $goodsList['dianzan'];
			if($dianzan){			
				return $this -> toJson(200, '点赞成功', $zannum);
			}else{
				return $this -> toJson(405, '数据异常');
			}
		}
		
	}
	
	/**
	 * 评论
	 */
	function getPinlun(){
		$assid = request() -> param('assid');
		$mem_uid = request() -> param('mem_uid');
		$mem_name = request() -> param('mem_name');
		$content = request() -> param('content');
		$param['assess_id'] = $assid;
		$param['uid'] = $mem_uid;
		$param['name'] = $mem_name;
		$param['add_time'] = now_time();
		$param['content'] = $content;
		$res = db('mall_assessitem')->insert($param);
		$update = db('mall_assess')->where('id',$assid)->setInc('num',1);
		if($res){			
			return $this -> toJson(200, '评论成功', '');
		}else{
			return $this -> toJson(405, '数据异常');
		}
	}
}
